package com.ciwong.epaper.modules.reciteWords.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.reciteWords.util.WordJumpManager;
import com.ciwong.libs.audio.play.AudioPlayer;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.utils.ToastUtil;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;

import static com.ciwong.epaper.modules.reciteWords.ui.ReciteWordDetailActivity.INTENT_FROM_WORD_STUDY_ACTIVITY;


public class EnglishToPicView  extends StudyWordMoudleView implements View.OnClickListener{
    private Context mContex;
    private WordDetail currentWord;
    private ArrayList<WordDetail> wordDetails=new ArrayList<WordDetail>();

    private ImageView item_a,item_b,item_c,item_d,item_refresh_a,
            item_refresh_b,item_refresh_c,item_refresh_d,
            item_a_result,item_b_result,item_c_result,item_d_result;
    private TextView  item_a_tv,item_b_tv,item_c_tv,item_d_tv,word_name,word_yibiao;

    private NextStepLinstener nextStepLinstener;
    private Handler mHandler = new Handler();
    private boolean isLock=true;

    public EnglishToPicView(Context context) {
        super(context);
        mContex=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_pic, this);
        initView();
    }

    public EnglishToPicView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContex=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_pic, this);
        initView();
    }

    public EnglishToPicView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContex=context;
        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_pic, this);
        initView();
    }



    @Override
    public void updataui() {
        isLock=true;
        item_a_result.setVisibility(GONE);
        item_b_result.setVisibility(GONE);
        item_c_result.setVisibility(GONE);
        item_d_result.setVisibility(GONE);

       /* item_refresh_a.setVisibility(GONE);
        item_refresh_b.setVisibility(GONE);
        item_refresh_c.setVisibility(GONE);
        item_refresh_d.setVisibility(GONE);*/
    }

    private void initView(){
        setOrientation(VERTICAL);
        item_a=findViewById(R.id.item_a);
        item_b=findViewById(R.id.item_b);
        item_c=findViewById(R.id.item_c);
        item_d=findViewById(R.id.item_d);
        item_refresh_a=findViewById(R.id.item_refresh_a);
        item_refresh_b=findViewById(R.id.item_refresh_b);
        item_refresh_c=findViewById(R.id.item_refresh_c);
        item_refresh_d=findViewById(R.id.item_refresh_d);
        item_a_result=findViewById(R.id.item_a_result);
        item_b_result=findViewById(R.id.item_b_result);
        item_c_result=findViewById(R.id.item_c_result);
        item_d_result=findViewById(R.id.item_d_result);
        item_a_tv=findViewById(R.id.item_a_tv);
        item_b_tv=findViewById(R.id.item_b_tv);
        item_c_tv=findViewById(R.id.item_c_tv);
        item_d_tv=findViewById(R.id.item_d_tv);
        word_name=findViewById(R.id.word_name);
        word_yibiao=findViewById(R.id.word_yibiao);

        item_a.setOnClickListener(this);
        item_b.setOnClickListener(this);
        item_c.setOnClickListener(this);
        item_d.setOnClickListener(this);
        item_refresh_a.setOnClickListener(this);
        item_refresh_b.setOnClickListener(this);
        item_refresh_c.setOnClickListener(this);
        item_refresh_d.setOnClickListener(this);
    }


    public void setDataAndUpdataUI(WordDetail currentWord,ArrayList<WordDetail> wordDetails,NextStepLinstener nextStepLinstener){
        try{
            if(currentWord==null||wordDetails.size()<4){
                return;
            }
            this.nextStepLinstener=nextStepLinstener;
            this.currentWord=currentWord;
            this.wordDetails=wordDetails;
            isLock=true;
            word_name.setText(currentWord.getWords());
            word_yibiao.setText("["+currentWord.getSymbol()+"]");
            item_a_tv.setText(wordDetails.get(0).getPretations());
            item_b_tv.setText(wordDetails.get(1).getPretations());
            item_c_tv.setText(wordDetails.get(2).getPretations());
            item_d_tv.setText(wordDetails.get(3).getPretations());

            item_a_result.setVisibility(GONE);
            item_b_result.setVisibility(GONE);
            item_c_result.setVisibility(GONE);
            item_d_result.setVisibility(GONE);

            item_refresh_a.setVisibility(GONE);
            item_refresh_b.setVisibility(GONE);
            item_refresh_c.setVisibility(GONE);
            item_refresh_d.setVisibility(GONE);

            if(!NetworkUtils.isOnline()){
                item_refresh_a.setVisibility(VISIBLE);
                item_refresh_b.setVisibility(VISIBLE);
                item_refresh_c.setVisibility(VISIBLE);
                item_refresh_d.setVisibility(VISIBLE);
            }else {
                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(0).getWordPic(),
                        item_a, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {
                                item_refresh_a.setVisibility(VISIBLE);
                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                item_refresh_a.setVisibility(GONE);
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });

                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(1).getWordPic(),
                        item_b, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {
                                item_refresh_b.setVisibility(VISIBLE);
                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                item_refresh_b.setVisibility(GONE);
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });

                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(2).getWordPic(),
                        item_c, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {
                                item_refresh_c.setVisibility(VISIBLE);
                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                item_refresh_c.setVisibility(GONE);
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });

                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(3).getWordPic(),
                        item_d, new ImageLoadingListener() {
                            @Override
                            public void onLoadingStarted(String s, View view) {

                            }

                            @Override
                            public void onLoadingFailed(String s, View view, FailReason failReason) {
                                item_refresh_d.setVisibility(VISIBLE);
                            }

                            @Override
                            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                item_refresh_d.setVisibility(GONE);
                            }

                            @Override
                            public void onLoadingCancelled(String s, View view) {

                            }
                        });

            }




        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("EnglishToPic");
        }




    }


    @Override
    public void onClick(View view) {
        try{
            if(view.getId()==R.id.item_a){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(0).getdId())){
                        isLock=false;
                        item_a_result.setVisibility(VISIBLE);
                        item_a_result.setImageResource(R.mipmap.right_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("EnglishToPic");

                            }
                        }, DELAY_TIME);
                        //跳转下一个

                    }else {
                        isLock=false;
                        item_a_result.setVisibility(VISIBLE);
                        item_a_result.setImageResource(R.mipmap.error_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);

                        //跳转详情

                    }
                }

            }else if(view.getId()==R.id.item_b){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(1).getdId())){
                        isLock=false;
                        item_b_result.setVisibility(VISIBLE);
                        item_b_result.setImageResource(R.mipmap.right_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("EnglishToPic");

                            }
                        }, DELAY_TIME);
                        //跳转下一个

                    }else {
                        isLock=false;
                        item_b_result.setVisibility(VISIBLE);
                        item_b_result.setImageResource(R.mipmap.error_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);


                        //跳转详情

                    }
                }


            }else if(view.getId()==R.id.item_c){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(2).getdId())){
                        isLock=false;
                        item_c_result.setVisibility(VISIBLE);
                        item_c_result.setImageResource(R.mipmap.right_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("EnglishToPic");

                            }
                        }, DELAY_TIME);
                        //跳转下一个

                    }else {
                        isLock=false;
                        item_c_result.setVisibility(VISIBLE);
                        item_c_result.setImageResource(R.mipmap.error_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);

                        //跳转详情

                    }
                }


            }else if(view.getId()==R.id.item_d){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(3).getdId())){
                        isLock=false;
                        item_d_result.setVisibility(VISIBLE);
                        item_d_result.setImageResource(R.mipmap.right_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("EnglishToPic");

                            }
                        }, DELAY_TIME);
                        //跳转下一个

                    }else {
                        isLock=false;
                        item_d_result.setVisibility(VISIBLE);
                        item_d_result.setImageResource(R.mipmap.error_img);
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);


                        //跳转详情

                    }
                }


            }
            else if(view.getId()==R.id.item_refresh_a){
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(0).getWordPic(),
                            item_a, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String s, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String s, View view, FailReason failReason) {
                                    item_refresh_a.setVisibility(VISIBLE);
                                }

                                @Override
                                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                    item_refresh_a.setVisibility(GONE);

                                }

                                @Override
                                public void onLoadingCancelled(String s, View view) {

                                }
                            });

                }

            }else if(view.getId()==R.id.item_refresh_b){
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(1).getWordPic(),
                            item_b, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String s, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String s, View view, FailReason failReason) {
                                    item_refresh_b.setVisibility(VISIBLE);
                                }

                                @Override
                                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                    item_refresh_b.setVisibility(GONE);
                                }

                                @Override
                                public void onLoadingCancelled(String s, View view) {

                                }
                            });

                }

            }else if(view.getId()==R.id.item_refresh_c){
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(2).getWordPic(),
                            item_c, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String s, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String s, View view, FailReason failReason) {
                                    item_refresh_c.setVisibility(VISIBLE);
                                }

                                @Override
                                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                    item_refresh_c.setVisibility(GONE);
                                }

                                @Override
                                public void onLoadingCancelled(String s, View view) {

                                }
                            });

                }

            }else if(view.getId()==R.id.item_refresh_d){
                if(!NetworkUtils.isOnline()){
                    ToastUtil.INSTANCE.toastCenterNoNetError();
                }else {
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(wordDetails.get(3).getWordPic(),
                            item_d, new ImageLoadingListener() {
                                @Override
                                public void onLoadingStarted(String s, View view) {

                                }

                                @Override
                                public void onLoadingFailed(String s, View view, FailReason failReason) {
                                    item_refresh_d.setVisibility(VISIBLE);
                                }

                                @Override
                                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                                    item_refresh_d.setVisibility(GONE);
                                }

                                @Override
                                public void onLoadingCancelled(String s, View view) {

                                }
                            });

                }

            }
        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("EnglishToPic");
        }


    }
}
